import { throttle } from "../utils/throttle";
import { commands, ProgressLocation, window } from "vscode";
import { getQuestionData } from "../apis/get-question-data";
import { languageService } from "../services/LanguageService";
import { questionDescriptionWebView } from "../webview/QuestionDescriptionWebView";
import { isError } from "../common/Errors";

/**
 * 打开题目
 */
export const openQuestion = throttle(async (title: string, titleSlug: string) => {
    commands.executeCommand("vscode.setEditorLayout", {orientation: 0, groups: [{size: 0.6}, {size: 0.4}]});
    return window.withProgress({location: ProgressLocation.Notification}, async p => {
        p.report({message: `打开题目 ${title} 中`});
        return getQuestionData(titleSlug).then(async res => {
            return languageService.openEditor(res).then(() => {
                questionDescriptionWebView.show(res);
            });
        }).catch((reason) => {
            console.log(reason);
            if (isError(reason)) {
                window.showWarningMessage(reason);
            }
        });
    });
});